package com.wicket.okrapp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wicket.okrapp.base.service.MOmsStandardDataResultService;
import com.wicket.okrapp.base.service.dto.req.AddCollectionFirstDataReq;
import com.wicket.okrapp.base.service.dto.req.BatchQueryStandardDataLIstReq;
import com.wicket.okrapp.base.service.dto.req.QueryStandardDataDetailReq;
import com.wicket.okrapp.base.service.dto.req.QueryStandardDataListReq;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.common.util.CommonFunctionHelper;
import com.wicket.okrapp.dal.mapper.mbg.OmsStandardDataResultMapper;
import com.wicket.okrapp.dal.po.mbg.OmsStandardDataResult;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MOmsStandardDataResultServiceImpl
        extends ServiceImpl<OmsStandardDataResultMapper, OmsStandardDataResult>
        implements MOmsStandardDataResultService {

    @Autowired
    OmsStandardDataResultMapper omsStandardDataResultMapper;

    /**
     * 2-4-01修改标准数据
     */
    @Trace(operationName = "2-4-01修改标准数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStandardData(OmsStandardDataResult omsStandardDataResult) {
        omsStandardDataResult.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsStandardDataResult.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsStandardDataResult);
    }

    /**
     * 2-4-01新增标准数据
     */
    @Trace(operationName = "2-4-01新增标准数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addStandardData(OmsStandardDataResult omsStandardDataResult) {
        omsStandardDataResult.setIsArchive("FALSE");
        omsStandardDataResult.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsStandardDataResult.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsStandardDataResult.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsStandardDataResult.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsStandardDataResult.setIsValid("FALSE");
        omsStandardDataResult.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsStandardDataResult.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsStandardDataResult)) {
            return omsStandardDataResult.getDataResultId();
        } else {
            return "-1";
        }

    }

    /**
     * M-集合取第一条数据（特殊方法）
     */
    @Trace(operationName = "M-集合取第一条数据（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsStandardDataResult addCollectionFirstData(AddCollectionFirstDataReq reqDto) {

        try {

            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            List<OmsStandardDataResult> retList = (List<OmsStandardDataResult>) super.listByMap(columnMap);
            if (retList == null || retList.size() <= 0) return null;
            OmsStandardDataResult ret = retList.get(0);

            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }

    }

    /**
     * 2-4-01查标准数据详情
     */
    @Trace(operationName = "2-4-01查标准数据详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsStandardDataResult queryStandardDataDetail(QueryStandardDataDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            List<OmsStandardDataResult> retList = (List<OmsStandardDataResult>) super.listByMap(columnMap);
            if (retList == null || retList.size() <= 0) return null;
            OmsStandardDataResult ret = retList.get(0);

            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }

    }

    /**
     * 2-4-01查询标准数据列表
     */
    @Trace(operationName = "2-4-01查询标准数据列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsStandardDataResult> queryStandardDataList(QueryStandardDataListReq reqDto) {

        try {
            QueryWrapper<OmsStandardDataResult> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getStandardDataList() == null || reqDto.getStandardDataList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("entity_id", reqDto.getStandardDataList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsStandardDataResult.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsStandardDataResult.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsStandardDataResult.class);
            CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsStandardDataResult>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-4-01批量查标准数据列表
     */
    @Trace(operationName = "2-4-01批量查标准数据列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsStandardDataResult> batchQueryStandardDataLIst(BatchQueryStandardDataLIstReq reqDto) {

        try {
            QueryWrapper<OmsStandardDataResult> queryWrapper = new QueryWrapper<>();

            if (reqDto.getStandardDataList() == null || reqDto.getStandardDataList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("entity_id", reqDto.getStandardDataList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsStandardDataResult.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsStandardDataResult.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsStandardDataResult.class);
            CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsStandardDataResult>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
